const connection = require('../../app/database')

class UserService {
  // 创建用户
  async create(user) {
    const { sno, nickname, password, phone, idCard, auth } = user
    const statement = `insert into user (sno, nickname, password, phone, idCard, auth) values (?, ?, ?, ?, ?, ?)`
    const result = await connection.execute(statement, [sno, nickname, password, phone, idCard, auth])
    return result[0]
  }

  // 查询用户
  async queryUser(user) {
    const { sno, nickname, phone, idCard } = user
    const statement = `select id from user where sno = ? or nickname = ? or phone = ? or idCard = ?`
    const result = await connection.execute(statement, [sno, nickname, phone, idCard])
    return result[0]
  }

  // 修改用户密码
  async updatePassword(id, password) {
    const statement = `update user set password = ? where id = ?`
    const result = await connection.execute(statement, [password, id])
    return result[0]
  }

  // 修改用户头像
  async updateAvatar(id, avatar) {
    const statement = `update user set avatar = ? where id = ?`
    const result = await connection.execute(statement, [avatar, id])
    return result[0]
  }

}

module.exports = new UserService()
